<?php

namespace Tools\Utils;

class Path
{
    /**
     * 上传路径转化,默认路径
     * @param $path
     * @param int $type
     * @param bool $force
     * @return string
     */
    public static function make_path($path, int $type = 2, bool $force = false)
    {
        $path = DIRECTORY_SEPARATOR . ltrim(rtrim($path));
        switch ($type) {
            case 1:
                $path .= DIRECTORY_SEPARATOR . date('Y');
                break;
            case 2:
                $path .= DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . date('m');
                break;
            case 3:
                $path .= DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
                break;
        }
        try {
            if (is_dir(app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'uploads' . $path, 0777, true) == true) {
                return trim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '.');
            } else return '';
        } catch (\Exception $e) {
            if ($force)
                throw new \Exception($e->getMessage());
            return '无法创建文件夹，请检查您的上传目录权限：' . app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'attach' . DIRECTORY_SEPARATOR;
        }

    }

    /**
     * 设置附加路径
     * @param $url
     * @return bool
     */
    public static function set_file_url($image, $siteUrl = '')
    {
        if (!strlen(trim($siteUrl))) $siteUrl = sys_config('site_url');
        $domainTop = substr($image, 0, 4);
        if ($domainTop == 'http') return $image;
        $image = str_replace('\\', '/', $image);
        return $siteUrl . $image;
    }

    /**
     * url转换路径
     * @param $url
     * @return string
     */
    public static function url_to_path($url)
    {
        $path = trim(str_replace('/', DIRECTORY_SEPARATOR, $url), DIRECTORY_SEPARATOR);
        if (0 !== strripos($path, 'public'))
            $path = 'public' . DIRECTORY_SEPARATOR . $path;
        return app()->getRootPath() . $path;
    }

    /**
     * 路径转url路径
     * @param $path
     * @return string
     */
    public static function path_to_url($path)
    {
        return trim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '.');
    }

    /**
     * 获取文件带域名的完整路径
     * @param string $link
     * @return string
     */
    public static function get_file_link(string $link)
    {
        if (!$link) {
            return '';
        }
        if (strstr('http', $link) === false) {
            return app()->request->domain() . $link;
        } else {
            return $link;
        }
    }

    /**
     * CURL 检测远程文件是否在
     * @param $url
     * @return bool
     */
    public static function curl_file_exist($url)
    {
        $ch = curl_init();
        try {
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
            $contents = curl_exec($ch);
            if (preg_match("/404/", $contents)) return false;
            if (preg_match("/403/", $contents)) return false;
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * public path
     *
     * @param string $path
     * @time 2020年09月08日
     * @return string
     */
    public static function publicPath($path = '')
    {
        return root_path($path ? 'public/' . $path : 'public');
    }
}
